
*********************************************************************
9/17/18: Added 2018 survey year.

9/23/17: Added 2017 survey year.

10/19/15: Added 2015 survey year.

10/21/14: Added 2014 survey year.

12/4/13: Added 2013 survey year.

12/21/12: Added 2012 survey year.  COMMENTED OUT THE EXCLUSOIN OF KIDS UNDER 15 AND FTYPE=5, AND 
          RAN FOR ALL YEARS 92-12

11/5/11: Added 2011 survey year.

8/3/11: ADDED WIC IMPUTATION.  NOTE, BEFORE SY 2001, WE DON'T IDENTIFY PREGNANT WOMEN
		ON WIC BECAUSE DON'T HAVE WICYN, SO WE EXCLUDE PREGNANT WOMEN FROM THE WIC BEN
		CALCULATION (THIS IS DOWN BY CONDITIONING ON FINFANT>0).  

4/30/11: Re-ran having added fssinc_rr so can look at effect of soical security

3/16/11: Re-ran having added pawval and tot_trans (total gov transfers)

12/30/10: Re-ran 2010 survey year now that imputed benefit vars 
		  are available.

12/21/10: Re-ran 2010 survey year now that state tax data available
		  from Taxsim:  Note, imputed benefit vars still unavailable.

11/5/10: Added 2010 survey year.

12/21/09: Added 2009 survey year.

9/4/09: Added 2007 & 2008 survey years.

1/9/08: Added 2006, and deleted extra housing stuff for 2004, b/c
	those data now come from data ferret.

8/23/07: I converted this to a macro and added the tax variables.

THESE NOTES WERE IN THE PROGRAM, BUT FOR SOME REASON, THIS SCREWS UP
THE MACRO, SO I'VE MOVED THEM TO THE TOP

* starting in 2000 we assign povcut of primary family to the related 
  subfamily because the for some reason the cps doesn't do this

* delete secondary individuals under 15, they're not in poverty measure

DEFINE SM'S AS:
	never married, divorced, separated, or widowed
	at least one own never-married kid under 18
	primary families or unrelated subfamilies 

* primary families (ftype=1), related subfamilies (ftype=3)

*  Give the value of food stamps and family income at the family level for those in 

**********************************************************************;

options obs=max mprint;

%macro clean9205(yr);

data poverty;
      set cpsdata.all&yr; 
/*	  if ftype=5 and age < 15 then delete; */
      lft5=floor(famid/100);
	  id=pseq*100+ppos;

/* THESE VARIABLES ARE ONLY AVAILABLE IN MORE RECENT YEARS */

	IF &yr LT 2011 THEN DO;
		fhip_val=.;
		fmoop=.;  
        fotc_val=.;
        fmed_val=.;
		chsp_val=.;
		fchsp_val=.; 
	END;
run;

PROC SORT DATA=poverty;
   BY id;
RUN;
PROC SORT DATA=taxsim.taxs&yr;
   BY id;
RUN;
DATA poverty;
MERGE poverty(IN=in1) taxsim.taxs&yr(DROP=year);
BY id;
IF in1;

ts_tottax=ftl+stl+(ts_fica/2);

run;

PROC SORT DATA=poverty;
   BY famid;
RUN;
proc means sum noprint data=poverty;
   by famid;
   var ts_tottax;
   output out=tmp1(drop= _type_ _freq_) sum=ftax_nonprime;
run;

DATA poverty;
MERGE poverty(IN=in1) tmp1(KEEP=famid ftax_nonprime);
BY famid;
IF in1;
run;

PROC SORT DATA=poverty;
   BY hseq;
RUN;
proc means sum noprint data=poverty;
   by hseq;
   where ftype IN(1,3);
   var ts_tottax;
   output out=tmp2(drop= _type_ _freq_) sum=ftax_prime;
run;
proc means sum noprint data=poverty;
   by hseq;
   var ts_tottax;
   output out=tmp3(drop= _type_ _freq_) sum=hts_tottax;
run;

DATA poverty;
MERGE poverty(IN=in1) tmp2(KEEP=hseq ftax_prime) tmp3(KEEP=hseq hts_tottax);
BY hseq;
IF in1;

IF ftype IN(1,3) THEN fts_tottax=ftax_prime;
   ELSE fts_tottax=ftax_nonprime;

run;


proc sort data=poverty;
by famid;
run;

data temp1;
set poverty;
by famid;
if first.famid;
   IF marstat IN (4,5,6,7) AND fownu18 GT 0 AND ftype IN (1,4) 
      and sex=2 and 18 LE age LE 54 then sm_f=1;
   ELSE sm_f=0;

keep year sm_f ftype fkind hseq famid refrel2;
run;
DATA poverty;
MERGE poverty(IN=in1) temp1(keep=famid sm_f) ;
by famid;
run;

proc sort data=poverty;
by hseq;
run;

proc means max noprint data=poverty;
by hseq;
var refrel2 ftype;
   output out=temp3 max=fammax typemax;
run;

data temp2;
set temp1(rename=(sm_f=sm_h));
   IF ftype=1;
run;


proc sort data=temp2;
by hseq;
run;

DATA poverty;
MERGE poverty(IN=in1) temp2(keep=hseq sm_h) temp3(keep=hseq fammax typemax);
by hseq;
   IF ftype ne 3 then sm=sm_f;
   ELSE IF ftype=3 then sm=sm_h;

   IF ftype=1 and sm=1 and fammax LE 5 and typemax=1 then smlone=1;
   ELSE smlone=0;
   
run;


data temp;
   set poverty; 
   if ftype=1;
run;

data temp;
   set temp; 
   by famid notsorted;
   if first.famid=1 then dummy=1;
   else dummy=0;
   if dummy=1;
keep lft5 ftotval fmvfs fhoussub fmvrsl fngmcare fngmcaid ftotearn fpersons frelu18 fpovcut fmoop;
run;

proc sort data=poverty;
by lft5;
run;
proc sort data=temp;
by lft5;
run;

data poverty;
merge poverty temp(rename=(ftotval=ftotval_new fmvfs=fmvfs_new
     fhoussub=fhoussub_new fmvrsl=fmvrsl_new 
     fngmcare=fngmcare_new fngmcaid=fngmcaid_new ftotearn=ftotearn_new 
	 fpersons=fpersons_new frelu18=frelu18_new fpovcut=fpovcut_new fmoop=fmoop_new));
by lft5;

if ftype=3 then do;
   ftotval_final=ftotval_new;
      fmvfs_final=fmvfs_new;
         fhoussub_final=fhoussub_new; 
         fmvrsl_final=fmvrsl_new;
         fngmcare_final=fngmcare_new; 
         fngmcaid_final=fngmcaid_new; 
         ftotearn_final=ftotearn_new;
		 fpersons_final=fpersons_new;
	     frelu18_final=frelu18_new;
		 fmoop_final=fmoop_new; 
end;
else if ftype ne 3 then do;
        ftotval_final=ftotval;
         fmvfs_final=fmvfs;
         fhoussub_final=fhoussub; 
         fmvrsl_final=fmvrsl;
         fngmcare_final=fngmcare; 
         fngmcaid_final=fngmcaid; 
		 ftotearn_final=ftotearn;
		 fpersons_final=fpersons;
	     frelu18_final=frelu18;
	     fmoop_final=fmoop; 
end;
		
IF &yr GE 2000 AND ftype=3 THEN fpovcut_final=fpovcut_new;
   ELSE IF &yr GE 2000 AND ftype ne 3 THEN fpovcut_final=fpovcut;

/* BEFORE SY 2001, WE DON'T IDENTIFY PREGNANT WOMEN ON WIC BECAUSE DON'T HAVE WICYN, 
   SO FWIC_MOM WILL BE EXCLUDING PREGNANT WOMEN AND ASSUMING FULL TAKEUP. */ 

fwic_mom=MIN(fpot_wic_mom,finfant,1);

IF famrel IN (1,2) AND sex=2 AND finfant GT 0 THEN wic_mom=1;
   ELSE wic_mom=0;

run;

proc means sum noprint data=poverty;
by lft5;
where ftype in(1,3);
var tottax eitc pcapgain pcaploss emcontrb tot_trans ssinc_rr mcaid_yes kid_0_4 wkswrk pawval num_wic wic_mom infant chsp_val;
output out=temp sum=ftottax2 feitc2 fcapgain2 fcaploss2 femcontrb2 ftot_trans2 fssinc_rr2
					fmcaid2 fkid_0_42 fwkswrk2 fpawval2 fnum_wic2 fwic_mom2 finfant2 fchsp_val2;
run;

data cpsdata.poverty&yr;
merge poverty temp (keep=lft5 ftottax2 feitc2 fcapgain2 fcaploss2 femcontrb2 ftot_trans2 fssinc_rr2
						 fmcaid2 fkid_0_42 fwkswrk2 fpawval2 fnum_wic2 fwic_mom2 finfant2 fchsp_val2);
by lft5; 

if ftype in (1,3) then do;
   ftottax_final=ftottax2;
   feitc_final=feitc2;
	fcapgain_final=fcapgain2;
     fcaploss_final=fcaploss2;
     femcontrb_final=femcontrb2;
	 ftot_trans_final=ftot_trans2;
	 fssinc_rr_final=fssinc_rr2;

	 fmcaid_final= fmcaid2;
	 fkid_0_4_final= fkid_0_42;
	 fwkswrk_final=fwkswrk2;
	 fpawval_final=fpawval2;
	 fnum_wic_final=fnum_wic2;
	 fwic_mom_final=fwic_mom2;
	 finfant_final=finfant2;
	 fchsp_val_final=fchsp_val2;
end;
else if ftype in (2,4,5) then do;
   ftottax_final=ftottax;
   feitc_final=feitc;
	fcapgain_final=fcapgain;
     fcaploss_final=fcaploss;
     femcontrb_final=femcontrb;
	 ftot_trans_final=ftot_trans;
	 fssinc_rr_final=fssinc_rr;

	 fmcaid_final= fmcaid;
	 fkid_0_4_final= fkid_0_4;
	 fwkswrk_final=fwkswrk;
	 fpawval_final=fpawval;
	 fnum_wic_final=fnum_wic;
	 fwic_mom_final=fwic_mom;
	 finfant_final=finfant;
	 fchsp_val_final=fchsp_val;
end;

run;
proc means;
run;

%mend;


%clean9205(1992);
%clean9205(1993);
%clean9205(1994);
%clean9205(1995);
%clean9205(1996);
%clean9205(1997);
%clean9205(1998);
%clean9205(1999);
%clean9205(2000);
%clean9205(2001);
%clean9205(2002);
%clean9205(2003);
%clean9205(2004);
%clean9205(2005);
%clean9205(2006);
%clean9205(2007);
%clean9205(2008);
%clean9205(2009);
%clean9205(2010);
%clean9205(2011);
%clean9205(2012);
%clean9205(2013);
%clean9205(2014);
%clean9205(2015);
%clean9205(2016);
%clean9205(2017);
%clean9205(2018);


* Bring in new weights for 93 from http://www.census.gov/housing/pwgts93.dat; 
  filename wgts93 'C:\Users\worri\Dropbox\Poverty\cpsdata\pwgts93.dat';
data wgtdat;
      infile wgts93 missover lrecl=25;
      input  pseq      1-5
	  		 ppos      6-7
             msupwgt   8-15;
ppos=ppos+40;
run;
proc sort data=wgtdat;
by pseq ppos;
proc sort data=cpsdata.poverty1993;
by pseq ppos;
data cpsdata.poverty1993;
   merge cpsdata.poverty1993(in=in1 drop=msupwgt) wgtdat;
by pseq ppos;
if in1;
run;
